package com.suny.sf_server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suny.Farm.dto.*;
import com.suny.Farm.vo.FarmSelectVO;
import com.suny.sf_server.service.FarmService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.suny.Farm.vo.FarmVO;
import com.suny.exception.BusinessException;
import com.suny.result.PageDTO;
import com.suny.result.PageVO;
import com.suny.result.R;

import java.util.List;


@Slf4j
@Tag(name = "牧场管理", description = "牧场管理相关接口")
@RestController
@RequestMapping("/farm")
public class FarmController {
    @Resource
    private FarmService farmService;
    
    @Operation(summary = "新增牧场")
    @PostMapping("/add")
    public R add(@Validated @RequestBody FarmAddDTO dto) {
        try {
            farmService.add(dto);
            return R.OK("新增牧场成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "修改牧场")
    @PostMapping("/update")
    public R update(@Validated @RequestBody FarmUpdateDTO dto) {
        try {
            farmService.update(dto);
            return R.OK("修改牧场成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "更新牧场状态")
    @PostMapping("/updateStatus")
    public R updateStatus(@Validated @RequestBody FarmUpdateStatusDTO dto) {
        try {
            Integer id = dto.getId();
            Integer status = dto.getStatus();
            farmService.updateStatus(id, status);
            return R.OK("更新状态成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
//    @Operation(summary = "获取牧场列表")
//    @PostMapping("/list")
//    public R getList(@Validated @RequestBody PageDTO pageDTO) {
//        try {
//            Page<FarmVO> page = farmService.getList(pageDTO);
//
//            // 构造返回的分页数据
//            PageVO<FarmVO> result = new PageVO<>();
//            result.setCurrent(page.getCurrent());
//            result.setSize(page.getSize());
//            result.setTotal(page.getTotal());
//            result.setPages(page.getPages());
//            result.setRecords(page.getRecords());
//
//            return R.OK(result);
//        } catch (BusinessException e) {
//            return R.FAIL(e.getMessage());
//        }
//    }
    
//    @Operation(summary = "获取可用牧场列表")
//    @GetMapping("/enable/list")
//    public R getEnableFarms() {
//        try {
//            List<FarmVO> list = farmService.getEnableFarms();
//            return R.OK(list);
//        } catch (BusinessException e) {
//            return R.FAIL(e.getMessage());
//        }
//    }
    
    @Operation(summary = "获取牧场详情")
    @PostMapping("/get")
    public R getDetail(@Validated @RequestBody FarmGetDTO farmGetDTO) {
        try {
            FarmVO vo = farmService.getDetail(farmGetDTO.getId());
            return R.OK(vo);
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    @Operation(summary = "下拉框获取可用牧场")
    @GetMapping("/select")
    public R getFarmBySelect() {
        try {
            List<FarmSelectVO> list = farmService.getFarmBySelect();
            return R.OK(list);
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "条件查询牧场列表")
    @PostMapping("/list")
    public R getListByCondition(@Validated @RequestBody FarmQueryDTO query) {
        log.info("接收到查询参数: {}", query);
        try {
            Page<FarmVO> page = farmService.getListByCondition(query);
            
            // 构造返回的分页数据
            PageVO<FarmVO> result = new PageVO<>();
            result.setCurrent(page.getCurrent());
            result.setSize(page.getSize());
            result.setTotal(page.getTotal());
            result.setPages(page.getPages());
            result.setRecords(page.getRecords());
            
            log.info("查询结果: total={}, size={}", page.getTotal(), page.getRecords().size());
            return R.OK(result);
        } catch (BusinessException e) {
            log.error("查询失败", e);
            return R.FAIL(e.getMessage());
        }
    }
}
